package com.edu.controller;

import com.edu.bean.Book;
import com.edu.service.BookService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:zyn
 * @Date:2025/11/21--13:12:36
 */
@Controller
@Transactional
public class BookController {

    @Resource
    private BookService bookService;
    @RequestMapping("/bookOperate")
    public ModelAndView deleteBook(int bookId,
                                                    @RequestParam(defaultValue = "1") int pageNum,
                                                    @RequestParam(defaultValue = "10") int pageSize){

        ModelAndView mv=new ModelAndView();
        int i = bookService.removeBookById(bookId);
        PageInfo pageInfo=bookService.getAllBook(pageNum,pageSize);

        if(i == 1){
            mv.setViewName("book.jsp");
            mv.addObject("pageInfo",pageInfo);
        }
        return mv;
    }

    @RequestMapping("/selectiveBook")
    public ModelAndView selectiveBook(String bookName,String bookWriter,String bookType,
                                      @RequestParam(defaultValue = "1") int pageNum,
                                      @RequestParam(defaultValue = "10") int pageSize){
        ModelAndView mv=new ModelAndView();
        Book book=new Book();
        book.setBookWriter(bookWriter);
        book.setBookName(bookName);
        book.setBookType(bookType);
        PageInfo pageInfo = bookService.gettiveBook(book,pageNum,pageSize);
        mv.setViewName("book.jsp");
        mv.addObject("pageInfo",pageInfo);
        return mv;
    }

    @RequestMapping("selectAllBook")
    public ModelAndView getAllBook( @RequestParam(defaultValue = "1") int pageNum,
                                    @RequestParam(defaultValue = "10") int pageSize){
        ModelAndView mv=new ModelAndView();
        PageInfo pageInfo = bookService.getAllBook(pageNum,pageSize);
        mv.setViewName("book.jsp");
        mv.addObject("pageInfo",pageInfo);
        return mv;
    }
}
